package com.mini.mysql.service;

import com.mini.mysql.model.BusinessStudentBigLessonXcx;
import com.mini.mysql.repository.BusinessStudentBigLessonXcxRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author anjl
 * @version 1.0
 * @date Created in 2019/12/5
 */
@Service
@Slf4j
public class BusinessStudentBigLessonXcxService {

    @Autowired
    private BusinessStudentBigLessonXcxRepository businessStudentBigLessonXcxRepository;


    public Integer countOrder(Date startTime, Date endTime) {
        return businessStudentBigLessonXcxRepository.countByUpdateTimeBetween(startTime, endTime);
    }

    public Page<BusinessStudentBigLessonXcx> page(Date startTime, Date endTime, Pageable pageable) {
        Specification<BusinessStudentBigLessonXcx> specification = (root, query, cb) -> {
            List<Predicate> list = new ArrayList<>();
            list.add(cb.between(root.get("updateTime"), startTime, endTime));
//            list.add(cb.equal(root.get("orderNo"), "E20200229132013007200001"));
//            list.add(cb.between(root.get(BusinessStudentBigLessonXcx.Fields.updateTime), startTime, endTime));
            return cb.and(list.toArray(new Predicate[list.size()]));
        };
        return businessStudentBigLessonXcxRepository.findAll(specification, pageable);
    }
}

